var num = 0;
var animals =null;
$(function () {
    $('.citylist').hide();
    // 百度地图API功能
    var map = new BMap.Map("allmap");
    var point = new BMap.Point();
    map.centerAndZoom();
    function myFun(result){
        var cityName = result.name;
        map.setCenter(cityName);
        var cName=cityName.substr(0,2);
        $(".dingwei").html(cName);
        $(".dingwei2").html(cName);
        getNowCityId();
    }
    var myCity = new BMap.LocalCity();
    myCity.get(myFun);
    getAllCity();
    getAllHotCity();


    //绑定父级类型
    bindFatherType();
    //点击类别上按钮  --------------------这里欠考虑，应该传入城市Id和商品Id
    $("#banner-one").on('click','.typeId',function () {
        //获取当前类型Id
        var id=this.id;
        //跳转页面，将类型Id参数
        var cityId=$(".dingwei").attr("id");
        var cityName=$(".dingwei").html();

        location.href="http://localhost:8080/static/html/action.html?id="+id+"&cityId="+cityId;
    });

    //点击儿童亲子，话剧歌剧更多按钮时
    $(".more").click(function () {
        //获取类别id
        var id=this.id;
        //跳转页面，将类型Id参数
        location.href="http://localhost:8080/static/html/action.html?id="+id;

    });

})

/**
 * 绑定当前城市id
 */
function getNowCityId(){
    var cityName=$(".dingwei").html();
    $.ajax({
        url: 'http://cn.pn.com/api-pn/base/cityPositioning1/p/findCity',
        dataType: 'json',
        type: 'Post',
        data: {cityName: cityName},
        success: function (data) {
            var cityId=data.data.cityId;

            bindDisShop(cityId,0);
            //绑定近期热门
            bindHotShop(cityId,0)
            //绑定歌剧
            bindGeJu(cityId,0,2);//首页加载是默认状态为0，也就是显示6条
            //绑定儿童亲子
            bindChild(cityId,0,6);//首页加载是默认状态为0，也就是显示6条
            $(".dingwei").attr("id",cityId);
        },error:function(){
            layer.msg("获取id错误");
        }
    });
}
function getAllHotCity(){
    $.ajax({
        url: 'http://cn.pn.com/api-pn/base/cityPositioning1/p/getInfoTopTen',
        dataType: 'json',
        type: 'Post',
        success: function (data) {
            $(".asdf").html("");
            $.each(data.data,function (i,e) {
                $(".asdf").append("<li class='city-item' id='"+e.cityId+"' data-name='"+e.cityEnName+"'>"+e.cityName+"</li>");
            })

            /**
             * 热门城市绑定点击事件
             */
            $(".asdf li").bind("click",function () {
                var cName=$(this).html();
                var cityId=$(this).attr("id");
                $(".dingwei").html(cName);
                $(".dingwei").attr("id",cityId);
                $(".dingwei2").html(cName);
                $(".dingwei2").attr("id",cityId);
                $(".citylist").hide();
                num=0;
            })
        },error:function () {
            layer.msg("错误");
        }
    })
}
/**
 * 绑定所有城市
 */
function getAllCity(){
    $.ajax({
        url: 'http://cn.pn.com/api-pn/base/cityPositioning1/p/getAllInfo',
        dataType: 'json',
        type: 'Post',
        success: function (data) {
            $(".items").html("");
            $.each(data.data,function (i,e) {
                var cityAbbrCode=e.cityAbbrCode;
                var toUpper=cityAbbrCode.toUpperCase();
                var cToUpper=toUpper.substr(0,1);
                $("#cityUl li").each(function (i) {
                    var szm=$(this).attr("id");
                    if(szm==cToUpper){
                        $(this).find(".items").append("<div class='item city-item' id='"+e.cityId+"' data-name='"+e.cityEnName+"'>"+e.cityName+"</div>");
                        return false;
                    }
                })
            })
            /**
             * 绑定城市点击事件
             */
            $(".city-item").bind("click",function () {
                var cName=$(this).html();
                var cityId=$(this).attr("id");
                $(".dingwei").html(cName);
                $(".dingwei").attr("id",cityId);
                $(".dingwei2").html(cName);
                $(".dingwei2").attr("id",cityId);
                $(".citylist").hide();
                num=0;
                bindDisShop(cityId,0);
                //绑定近期热门
                bindHotShop(cityId,0)
                //绑定歌剧
                bindGeJu(cityId,0,2);//首页加载是默认状态为0，也就是显示6条
                //绑定儿童亲子
                bindChild(cityId,0,6);//首页加载是默认状态为0，也就是显示6条

            })
        },error:function () {
            layer.msg("绑定城市错误");
        }
    })
}


/**
 * 显示隐藏
 */
$('.city-name').click(function(){
    if (  num == 0) {
        num=1
        $('.citylist').show();
        //滚动条
        animals=[
            {
                'zm':'A',
                'zmTop':$("#A").offset().top
            },{
                'zm':'B',
                'zmTop':$("#B").offset().top
            },{
                'zm':'C',
                'zmTop':$("#C").offset().top
            },{
                'zm':'D',
                'zmTop':$("#D").offset().top
            },{
                'zm':'F',
                'zmTop':$("#F").offset().top
            },{
                'zm':'G',
                'zmTop':$("#G").offset().top
            },{
                'zm':'H',
                'zmTop':$("#H").offset().top
            },{
                'zm':'J',
                'zmTop':$("#J").offset().top
            },{
                'zm':'K',
                'zmTop':$("#K").offset().top
            },{
                'zm':'L',
                'zmTop':$("#L").offset().top
            },{
                'zm':'M',
                'zmTop':$("#M").offset().top
            },{
                'zm':'N',
                'zmTop':$("#N").offset().top
            },{
                'zm':'P',
                'zmTop':$("#P").offset().top
            },{
                'zm':'Q',
                'zmTop':$("#Q").offset().top
            },{
                'zm':'R',
                'zmTop':$("#R").offset().top
            },{
                'zm':'S',
                'zmTop':$("#S").offset().top
            },{
                'zm':'T',
                'zmTop':$("#T").offset().top
            },{
                'zm':'W',
                'zmTop':$("#W").offset().top
            },{
                'zm':'X',
                'zmTop':$("#X").offset().top
            },{
                'zm':'Y',
                'zmTop':$("#Y").offset().top
            },{
                'zm':'Z',
                'zmTop':$("#Z").offset().top
            }
        ]
    }else{
        num = 0
        $('.citylist').hide();
    }
})

$(".cap").click(function () {
    var zm=$(this).html();
    $.each(animals, function(index,value){
        if(zm==value.zm){
            $(".hot-cities").animate({
                scrollTop:value.zmTop-$('.hot-cities').offset().top }, {duration: 500,easing: "swing"});
            return false;
        }
    });
})




//绑定父级类型
function bindFatherType() {

    $.ajax({
        type: "Get",
        url: "http://cn.pn.com/api-pn/item/type/p/listAll",
        dataType: "json",
        success: function(data){

            var fType=$("#banner-one ul");
            $.each(data,function (i,e) {
                var $li="<li><div></div><a href=\"javascript:;\" id='"+e.showTypeId+"' class='typeId'>"+e.typeName+"</a><img src=\"../img/1.png\" class=\"img\"></li>";
                fType.append($li);

            });
        },
        error: function(msg){
            alert( "网络连接异常！！！");
        }
    });
}

//绑定折扣商品
function bindDisShop(cityId,statusId) {
    $.ajax({
        type: "Post",
        url: "http://cn.pn.com/api-pn/item/dis/p/getDisCommodityListByCityId",
        data:{cityId:cityId,statusId:statusId},
        dataType: "json",
        success: function(data){

            var $ul=$("#disShop");
            $ul.html("");
            $.each(data,function (i,e) {
                $.each(e.skuList,function (index,e2) {

                    var $li="<li><img src="+e.imgUrl+"><span>"+e2.discount+"<br><b>折</b></span><a href=\"javascript:;\">["+e.cityName+"] "+e.commodityName+"</a><p><i>￥</i>"+e2.disPrice+"<b>￥"+e2.price+"</b></p></li>";
                    $ul.append($li);
                });
            });
        },
        error: function(msg){
            alert( "折扣网络连接异常！！！");
        }
    });
}

//绑定热门商品
function bindHotShop(cityId,statusId){
    $.ajax({
        type: "Post",
        url: "http://cn.pn.com/api-pn/item/dis/p/getHotCommodityListByCityId",
        data:{cityId:cityId,statusId:statusId},
        dataType: "json",
        success: function(data){

            var $ul=$("#hotShop");
            $ul.html("");
            $.each(data,function (i,e) {
                $.each(e.skuList,function (index,e2) {
                    var $li="<li><img src="+e.imgUrl+"><span><b>"+e2.shopCount+"</b><br>🔥想看</span><a href=\"javascript:;\">["+e.cityName+"]"+e.commodityName+" </a><p><i>￥</i>"+e2.disPrice+"<b> 起</b></p></li>";
                    $ul.append($li);
                });
            });
        },
        error: function(msg){
            alert( "热门网络连接异常！！！");
        }
    });
}


/*绑定歌剧*/
function bindGeJu(cityId,statusId,fatherTypeId){
    $.ajax({
        type: "Post",
        url: "http://cn.pn.com/api-pn/item/dis/p/getCommodityMapByFatherList",
        data:{cityId:cityId,statusId:statusId,fatherTypeId:fatherTypeId},
        dataType: "json",
        success: function(data){
            var $bindGeJu=$("#bindGeJu");
            $bindGeJu.html("");
            $.each(data,function (i,e){
                $.each(e.skuList,function (index,e2) {
                    var $li="<li><img src="+e.imgUrl+"><p>["+e.cityName+"]"+e.commodityName+"<br><i>¥</i> <b>"+e2.disPrice+"</b> <em>起</em></p></li>";
                    $bindGeJu.append($li);
                })
            })
        },
        error: function(msg){
            alert( "歌剧网络连接异常！！！");
        }
    });
}

/*绑定儿童亲子*/
function bindChild(cityId,statusId,fatherTypeId){
    $.ajax({
        type: "Post",
        url: "http://cn.pn.com/api-pn/item/dis/p/getCommodityMapByFatherList",
        data:{cityId:cityId,statusId:statusId,fatherTypeId:fatherTypeId},
        dataType: "json",
        success: function(data){
            var $bindChild=$("#bindChild");
            $bindChild.html("");
            $.each(data,function (i,e){
                $.each(e.skuList,function (index,e2) {
                    var $li="<li><img src="+e.imgUrl+"><p>["+e.cityName+"]"+e.commodityName+"<br><i>¥</i> <b>"+e2.disPrice+"</b> <em>起</em></p></li>";
                    $bindChild.append($li);
                })
            })
        },
        error: function(msg){
            alert( "儿童网络连接异常！！！");
        }
    });
}